package com.ys.controller.api.finance;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ys.config.MenuId;
import com.ys.entity.finance.TcwDepositPlatform;
import com.ys.entity.sys.YsCol;
import com.ys.service.finance.TcwDepositPlatformService;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService;
import com.ys.util.ExportExcelUtils;
import com.ys.util.admin.ConditionUtil;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;

@Controller
@RequestMapping("/api/inter/tcwDepositPlatform")
public class ApiInterTcwDepositPlatformController {
    private static final Logger logger = LoggerFactory.getLogger(ApiInterTcwDepositPlatformController.class);

    @Autowired
    private CommonApiService<TcwDepositPlatform> commonApiService;

    @Autowired
    private TcwDepositPlatformService tcwDepositPlatformService;
    
    /**
     * 设置查询的cond的sql设置
     * @param cond
     * @return
     */
    public TcwDepositPlatform setSeachSql(TcwDepositPlatform cond){


        cond = commonApiService.dataAuth(MenuId.TCW_DepositPlatform, cond);
        return  cond;
    }

    @RequiresPermissions("tcwDepositPlatform:view")
    @RequestMapping("/search")
    @ResponseBody
    public LayuiTableVoJA search(@ModelAttribute TcwDepositPlatform cond){

        cond.setOrderby("A.createTime DESC")
            .addField("ci.Vname AS companyVname")
            .addLeftJoin("TB_CompanyInfo ci ON ci.Vid = A.CompanyVid");
        return commonApiService.searchLay(cond);
    }



    /**
     * 新增
     */
    @RequestMapping("/add")
    @ResponseBody
    public R toAdd(HttpServletRequest request, @RequestParam Map<String, Object> params
    		) {
        try {
            logger.info("新增传参：" + params);
            //修改
            return tcwDepositPlatformService.add(params);
        } catch (Exception e) {
            logger.error("新增出错：", e.getMessage(), e);
            return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/edit")
    @ResponseBody
    public R toEdit(HttpServletRequest request, @RequestParam Map<String, Object> params
    ) {
        try {
            logger.info("修改传参：" + params);
            //修改
            return tcwDepositPlatformService.edit(params);
        } catch (Exception e) {
            logger.error("修改出错：", e.getMessage(), e);
            return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
        }
    }

    /**
     * 货币兑换调用收款平台
     * @param cond
     * @return
     */
    @RequestMapping("/searchb")
    @ResponseBody
    public LayuiTableVoJA searchb(@ModelAttribute TcwDepositPlatform cond){
        cond.addLeftJoin("TB_CompanyInfo tci ON tci.vid = A.CompanyVid ")//公司
                .addField("tci.vname AS companyName");
        cond.setOrderby("A.createTime DESC");
        return commonApiService.searchLay(cond);
    }


    /**
     * 弹出框-收款平台账号管理
     */
    @RequestMapping("/searchForModalByAccount")
    @ResponseBody
    public LayuiTableVoJA searchForModalByAccount(@ModelAttribute TcwDepositPlatform cond){
        cond.setStatus(1);
        cond.addLeftJoin("TB_CompanyInfo O ON O.Vid = A.CompanyVid ")//公司信息
                .addField("O.Vname AS vname,O.vid AS companyVid");//所属公司简称,名称

        //已新增的不能再信息
        cond.where("( SELECT COUNT ( 1 ) AS num FROM TCW_WanliFinance B WHERE B.DepositPlatformCode = A.Code ) = 0 ");

        return commonApiService.searchLay(cond);
    }


    /**
     * 弹出框
     */
    @RequestMapping("/searchForModal")
    @ResponseBody
    public LayuiTableVoJA searchForModal(@ModelAttribute TcwDepositPlatform cond){
        cond.setStatus(1);
        cond.addLeftJoin("TB_CompanyInfo O ON O.Vid = A.CompanyVid ")//公司信息
                .addField("O.Vname AS vname,O.vid AS companyVid")
                .addField("O.Vname companyVname");

        return commonApiService.searchLay(cond);
    }
    /**
     * 导出
     */
    @RequestMapping("/export")
    @ResponseBody
    public R export(@ModelAttribute TcwDepositPlatform cond, String conditions){
        try {
            cond.setPage(0).setSize(999999);
            cond = setSeachSql(cond);
            //查询条件
            String nameLinked = ConditionUtil.getConditionValue(conditions, "_ys_createUserName");//制单人
            if (nameLinked != null){
                cond.where("U.UserId LIKE '%" + nameLinked + "%'" + " OR  U.UserName LIKE '%" + nameLinked + "%'");
            }
            //获取菜单字段数据
            List<YsCol> listField = new YsCol()
                    .setMenuId(MenuId.TCW_DepositPlatform)
                    .setFrontShow(1)
                    .setOrderby("A.SortNo asc")
                    .queryList();
            //获取数据
            List<Map<String, Object>> listData = cond.queryMap();

            //处理数据
            ExportExcelUtils<TcwDepositPlatform> util = new ExportExcelUtils<>(new TcwDepositPlatform());
            String toFilePath = util.exportExcel(listData, listField, "收款平台账号管理");
            UploadService.UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
            return R.ok(dto.getUrl());
        }catch (Exception e){
            logger.error("导出出错：",e.getMessage(),e);
            return R.error("导出失败，请稍后重试！");
        }
    }

}
